package erp

import (
	"context"
	"encoding/json"
	"ez/apps/erp/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type Inquiry struct {
	mgo.BaseDoc  `bson:"-" json:"-"`
	Id           int64             `json:"id" bson:"id,omitempty"`
	Sn           string            `json:"sn" bson:"sn"`                     //单据编号
	Title        string            `json:"title" bson:"title"`               //单据主题
	PayType      *PayType          `json:"payType" bson:"payType"`           //付款方式
	PayTypeId    int64             `json:"payTypeId" bson:"payTypeId"`       //付款方式
	Deliver      string            `json:"deliver" bson:"deliver"`           //交货地点
	ContactEmp   *Emp              `json:"contactEmp" bson:"contactEmp"`     //业务员
	ContactEmpId int64             `json:"contactEmpId" bson:"contactEmpId"` //业务员
	Contact      string            `json:"contact" bson:"contact"`           //联系人
	ContactPhone string            `json:"contactPhone" bson:"contactPhone"` //联系电话
	Status       *InquiryStatus    `json:"status" bson:"status"`             //状态
	StatusId     int64             `json:"statusId" bson:"statusId"`         //状态
	IsCancel     bool              `json:"isCancel" bson:"isCancel"`         //是否取消
	IsClose      bool              `json:"isClose" bson:"isClose"`           //是否关闭
	IsConfirm    bool              `json:"isConfirm" bson:"isConfirm"`       //是否生效
	ConfirmAt    time.Time         `json:"confirmAt" bson:"confirmAt"`       //生效时间
	FormAt       time.Time         `json:"formAt" bson:"formAt"`             //制单时间
	FormEmp      *Emp              `json:"formEmp" bson:"formEmp"`           //制单人
	FormEmpId    int64             `json:"formEmpId" bson:"formEmpId"`       //制单人
	CreateEmp    *Emp              `json:"createEmp" bson:"createEmp"`       //创建人
	CreateEmpId  int64             `json:"createEmpId" bson:"createEmpId"`   //创建人
	Note         string            `json:"note" bson:"note"`                 //备注
	Product      []*InquiryProduct `json:"product" bson:"product"`           //产品
	CreateAt     time.Time         `json:"createAt" bson:"createAt"`
	UpdateAt     time.Time         `json:"updateAt" bson:"updateAt"`
	DeleteAt     *time.Time        `json:"deleteAt" bson:"deleteAt"`
}

func (this *Inquiry) DocName() string { return "Inquiry" }
func (this *Inquiry) GetId() int64    { return this.Id }
func (this *Inquiry) SetId(id int64)  { this.Id = id }
func (this *Inquiry) Create() error {
	return this.GetFactory().Create(this)
}
func (this *Inquiry) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *Inquiry) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *Inquiry) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *Inquiry) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *Inquiry) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *Inquiry) ToString() string {
	return string(this.ToBytes())
}
func (this *Inquiry) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *Inquiry) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *Inquiry) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *Inquiry) LoadPayType() {
	if this.PayTypeId == 0 {
		return
	}
	this.PayType, _ = NewPayTypeCrud().FindId(this.PayTypeId)
}
func (this *Inquiry) LoadContactEmp() {
	if this.ContactEmpId == 0 {
		return
	}
	this.ContactEmp, _ = NewEmpCrud().FindId(this.ContactEmpId)
}
func (this *Inquiry) LoadStatus() {
	if this.StatusId == 0 {
		return
	}
	this.Status, _ = NewInquiryStatusCrud().FindId(this.StatusId)
}
func (this *Inquiry) LoadFormEmp() {
	if this.FormEmpId == 0 {
		return
	}
	this.FormEmp, _ = NewEmpCrud().FindId(this.FormEmpId)
}
func (this *Inquiry) LoadCreateEmp() {
	if this.CreateEmpId == 0 {
		return
	}
	this.CreateEmp, _ = NewEmpCrud().FindId(this.CreateEmpId)
}
func (this *Inquiry) LoadProduct() {
	product, _ := NewInquiryProductCrud().FindBy(ss.M{"belongToId": this.Id}, []string{"id"}, 0, 0)
	this.Product = product
}
func (this *Inquiry) ClearRelationsBeforeSave() mgo.Doc {
	this.PayType = nil
	this.ContactEmp = nil
	this.Status = nil
	this.FormEmp = nil
	this.CreateEmp = nil
	this.Product = nil
	return this
}
func neverUsed_Inquiry() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type InquiryAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *Inquiry
	Session *ez.Session
}

func NewInquiryAccessControl(model *Inquiry, action string, session *ez.Session) *InquiryAccessControl {
	ctrl := &InquiryAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.InquiryAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
